package com.xgpro.mobile.studycentre.controller;

import com.xgpro.common.controller.BaseController;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.results.ResponseResult;
import com.xgpro.common.utils.ReadPropertiesUtil;
import com.xgpro.common.utils.SendMsgUtil;
import com.xgpro.domain.messagemanage.entity.Message;
import com.xgpro.domain.rolemanage.entity.Department;
import com.xgpro.domain.studycentre.entity.Course;
import com.xgpro.domain.studycentre.vo.*;
import com.xgpro.domain.talentintroduction.entity.User;
import com.xgpro.mobile.studycentre.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/25 12:56
 */
@RestController
@RequestMapping("/course")
@CrossOrigin(allowCredentials="true")
public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;


    /**
     * 课程轮播图
     * @return
     */
    @RequestMapping("/qryCourseListForCarousel")
    public ResponseResult qryCourseListForCarousel(){
        List<Course> courses = courseService.qryCourseListForCarousel();
        return ResponseResult.ok(courses);
    }

    /**
     * 新增课程
     * @param request
     * @param courseFront
     * @return
     */
    @RequestMapping("/addCourse")
    public ResponseResult addCourse(HttpServletRequest request, CourseFront courseFront) throws ParseException {
        try{
            Integer id =(Integer) request.getSession().getAttribute("id");
            Integer deptId=(Integer) request.getSession().getAttribute("deptId");
            if(id==null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            if(deptId==null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            Integer result = null;
            try {
                result = courseService.addCourse(courseFront,id,deptId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseResult.ok(result);
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 根据状态区分课程
     * @param courseStatus
     * @return
     */
    @RequestMapping("/qryCourseByStatusForApproval")
    public ResponseResult qryCourseByStatusForApproval(HttpSession session,@RequestBody CourseStatus courseStatus){
        try {
            List<Integer> roleIds=(List<Integer>)session.getAttribute("roleIds");
            if (roleIds == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            Integer deptId = (Integer) session.getAttribute("secondDeptId");
            if (deptId == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(courseService.qryCourseByStatusForApproval(courseStatus, deptId,roleIds));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 根据id查询课程详细信息
     * @param id
     * @return
     */
    @RequestMapping("/qryCourseInfoById")
    public ResponseResult qryCourseInfoById(Integer id){
        return ResponseResult.ok(courseService.qryCourseInfoById(id));
    }

    /**
     * 课程通过
     * @param id
     * @return
     */
    @RequestMapping("/passCourse")
    public ResponseResult passCourse(HttpSession httpSession,Integer id) throws Exception {
        try{
            List<Integer> roleIds=(List<Integer>)httpSession.getAttribute("roleIds");
            if (roleIds == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            Integer result = courseService.passCourse(id, roleIds);
            if(result==0){
                throw new BusinessRuntimeException(ResultCodeEnum.APPROVAL_FAIL.getCode(),ResultCodeEnum.APPROVAL_FAIL.getMessage());
            }
            return ResponseResult.ok(result);
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 驳回课程
     * @param httpSession
     * @param id
     * @return
     */
    @RequestMapping("/refCourse")
    public ResponseResult refCourse(HttpSession httpSession,Integer id) throws Exception {
        try{
            List<Integer> roleIds=(List<Integer>)httpSession.getAttribute("roleIds");
            if (roleIds == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            Integer result=courseService.refCourse(id,roleIds);
            if(result==0){
                throw new BusinessRuntimeException(ResultCodeEnum.APPROVAL_FAIL.getCode(),ResultCodeEnum.APPROVAL_FAIL.getMessage());
            }
            return ResponseResult.ok(result);
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 我的课程待讲
     * @param session
     * @return
     */
    @RequestMapping("/openMyCourseList")
    public ResponseResult openMyCourseList(HttpSession session,Integer isOpen){
        try{
            Integer id=(Integer)session.getAttribute("id");
            if(id==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
           return ResponseResult.ok(courseService.openMyCourseList(id,isOpen));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }


    /**
     * 课程发布通知
     * @param courseMessage
     * @return
     */
    @RequestMapping("/openMyCourse")
    public ResponseResult openMyCourse(@RequestBody CourseMessage courseMessage) throws Exception {
         courseService.openMyCourse(courseMessage);
         return ResponseResult.ok();
    }

    /**
     * 我的课程审批中
     * @param session
     * @return
     */
    @RequestMapping("/appMyCourseList")
    public ResponseResult appMyCourseList(HttpSession session){
        try{
            Integer id=(Integer)session.getAttribute("id");
            if(id==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(courseService.appMyCourseList(id));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }



    /**
     * 我的课程完成
     * @param httpSession
     * @return
     */
    @RequestMapping("/achieveMyCourseList")
    public ResponseResult achieveMyCourseList(HttpSession httpSession){
        try{
            Integer id=(Integer)httpSession.getAttribute("id");
            if(id==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(courseService.achieveMyCourseList(id));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 提醒管理员审批
     * @param id
     * @return
     */
    @RequestMapping("/remindApproval")
    public ResponseResult remindApproval(Integer id) throws Exception {
        try{
            courseService.remindApproval(id);
            return ResponseResult.ok();
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return  ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 查询报名课程
     * @param session
     * @param courseSignFront
     * @return
     */
    @RequestMapping("/qrySignCourse")
    public ResponseResult qrySignCourse(HttpSession session,@RequestBody CourseSignFront courseSignFront){
        try{
            Integer userId=(Integer)session.getAttribute("id");
            if(userId==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return  ResponseResult.ok(courseService.qrySignCourse(courseSignFront,userId));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }

    }

    /**
     * 报名
     * @param httpSession
     * @param courseId
     * @return
     */
    @RequestMapping("/signOneCourse")
    public ResponseResult signOneCourse(HttpSession httpSession,Integer courseId){
        try{
            Integer userId=(Integer)httpSession.getAttribute("id");
            if(userId==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return  ResponseResult.ok(courseService.signOneCourse(userId,courseId));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 参加和未参加的课程
     * @param httpSession
     * @param mySignCourseFront
     * @return
     */
    @RequestMapping("/qryMySignCourse")
    public ResponseResult qryMySignCourse(HttpSession httpSession,@RequestBody MySignCourseFront mySignCourseFront){
        try{
            Integer userId=(Integer)httpSession.getAttribute("id");
            if(userId==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return  ResponseResult.ok(courseService.qryMySignCourse(mySignCourseFront,userId));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 课程中心列表
     * @param courseCentreFront
     * @return
     */
    @RequestMapping("/qryCourseCentre")
    public ResponseResult qryCourseCentre(@RequestBody CourseCentreFront courseCentreFront){
           return ResponseResult.ok(courseService.qryCourseCentre(courseCentreFront));
    }

    /**
     * 导师详情查看
     * @param id
     * @return
     */
    @RequestMapping("/qryTeacherByUserId")
    public ResponseResult qryTeacherByUserId(Integer id){
        return ResponseResult.ok(courseService.qryTeacherByUserId(id));
    }

    /**
     * 查看自己的课程
     * @param session
     * @param myCourse
     * @return
     */
    /*@RequestMapping("/qryMyCourse")
    public ResponseResult qryMyCourse(HttpSession session, @RequestBody MyCourse myCourse){
        try{
            Integer userId=(Integer) session.getAttribute("id");
            return ResponseResult.ok(courseService.qryMyCourse(userId,myCourse));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
        }
    }*/

    /**
     * 结课
     * @param overCourseFront
     * @return
     */
    @RequestMapping("/overStudy")
    public ResponseResult overStudy(@RequestBody OverCourseFront overCourseFront) throws Exception {
        try{
            Integer result = courseService.overStudy(overCourseFront);
            if(result==0){
                throw new BusinessRuntimeException(ResultCodeEnum.UPLOAD_FALE.getCode(),ResultCodeEnum.UPLOAD_FALE.getMessage());
            }
            return ResponseResult.ok(result);
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 查询部门相关信息(搜索)
     * @param id
     * @return
     */
    @RequestMapping("/qryCourseDeptMessage")
    public ResponseResult qryCourseDeptMessage(@RequestParam String id ,String userName)  {
        List<Department> departments = courseService.qryCourseDeptMessage(id ,userName);
        return ResponseResult.ok(departments);
    }

    /**
     * 查询部门及部门所属人员信息(搜索)
     * @param id
     * @param userName
     * @return
     */
    @RequestMapping("/qryCourseDeptUserList")
    public ResponseResult qryCourseDeptUserList(@RequestParam String id ,String userName)  {
        List<User> users = courseService.qryCourseDeptUserList(id,userName);
        return ResponseResult.ok(users);
    }
    /**
     * 查询部门相关信息
     * @param name
     * @return
     */
    @RequestMapping("/qryLikeDeptMessage")
    public ResponseResult qryLikeDeptMessage(@RequestParam String name )  {
        List<Department> departments = courseService.qryLikeDeptMessage(name);
        return ResponseResult.ok(departments);
    }

    /**
     * 查询部门相关信息
     * @param id
     * @return
     */
    @RequestMapping("/qryDeptMessage")
    public ResponseResult qryDeptMessage(@RequestParam String id )  {
        List<Department> departments = courseService.qryDeptMessage(id);
        return ResponseResult.ok(departments);
    }

    /**
     * 查询部门及部门所属人员信息
     * @param id)
     * @return
     */
    @RequestMapping("/qryDeptUserList")
    public ResponseResult qryDeptUserList(@RequestParam String id )  {
        List<User> users = courseService.qryDeptUserList(id);
        return ResponseResult.ok(users);
    }


    /**
     * 课程评估
     * @param map
     * @return
     */
    @RequestMapping("/courseEva")
    public ResponseResult courseEva(HttpSession httpSession,@RequestBody Map map){
        try{
            Integer id =(Integer) httpSession.getAttribute("id");
            if(id==null||"".equals(id)){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(courseService.courseEva(map,id));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 查询外部导师
     * @param name
     * @return
     */
    @RequestMapping("/qryOutUserList")
    public ResponseResult qryOutUserList(@RequestParam String name)  {
        List<User> users = courseService.qryOutUserList(name);
        return ResponseResult.ok(users);
    }

    /**
     * 微课程查看
     * @param session
     * @param id
     * @return
     */
    @RequestMapping("/qrySmallCourseInfo")
    public ResponseResult qrySmallCourseInfo(HttpSession session,Integer id){
        try{
            Integer userId =(Integer) session.getAttribute("id");
            if(userId==null||"".equals(userId)){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(courseService.qrySmallCourseInfo(id,userId));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 扫码签到
     * @param httpSession
     * @param courseId
     * @return
     */
    @RequestMapping("/doQR")
    public String doQR(HttpSession httpSession,Integer courseId){
        Integer id =(Integer) httpSession.getAttribute("id");
        if(id==null||"".equals(id)){
            return "签到失败";
        }
        return courseService.doQR(courseId,id);
    }

    /**
     * 首页精品课程
     * @return
     */
    @RequestMapping("/getGoodCourses")
    public ResponseResult getGoodCourses(){
        return ResponseResult.ok(courseService.getGoodCourses());
    }


    /**
     * 待审批条数
     * @param session
     * @return
     */
    @RequestMapping("/getCouseAppNum")
    public ResponseResult getCouseAppNum(HttpSession session){
        CourseStatus courseStatus = new CourseStatus();
        courseStatus.setStatus(0);
        courseStatus.setPage(1);
        courseStatus.setPageSize(10000);
        try {
            List<Integer> roleIds=(List<Integer>)session.getAttribute("roleIds");
            if (roleIds == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            Integer deptId = (Integer) session.getAttribute("secondDeptId");
            if (deptId == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(courseService.qryCourseByStatusForApproval(courseStatus, deptId,roleIds).size());
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }


}
